define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'supplier/index' + location.search,
                    add_url: 'supplier/add',
                    edit_url: 'supplier/edit',
                    del_url: 'supplier/del',
                    multi_url: 'supplier/multi',
                    import_url: 'supplier/import',
                    table: 'supplier',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'goods_name', title: "主供物料名称", operate: 'LIKE'},
                        {field: 'supply_type', title: __('Supply_type'), searchList: {"原料":__('原料'),"包材":__('包材')}, formatter: Table.api.formatter.normal},
                        {field: 'delivery_time', title: __('Delivery_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'linkman', title: __('Linkman'), operate: 'LIKE'},
                        {field: 'contact', title: __('Contact'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var $supplyType = $('#c-supply_type');
            var $goodsId = $('#c-goods_id');
            console.log("Aichif");
            
            // 监听下拉框变化事件
            $supplyType.on('change', function() {
                var supplyType = $(this).val();
                var apiUrl = '';
                console.log(supplyType);
                
                // 根据不同的 supply_type 设置不同的 API 地址
                switch(supplyType) {
                    case '原料': // 假设 type1 是你的 supply_type 值
                        apiUrl = 'clothmaking_raw/getRawInfo';
                        console.log("原料切换");
                        break;
                    case '包材':
                        apiUrl = 'package_material/getPackageMaterialInfo';
                        console.log("包材切换");
                        break;

                }
                $goodsId.data("selectPageObject").option.data = apiUrl;
                $goodsId.selectPageRefresh();;
                $goodsId.selectPageClear();
            });
            Controller.api.bindevent();
        },
        edit: function () {
            var $supplyType = $('#c-supply_type');
            var $goodsId = $('#c-goods_id');
            $("#c-goods_id").data("params", function (obj) {
                    //obj为SelectPage对象
                    return {type: $('#c-supply_type').val() == '原料' ? 0 : 1};
            });
            // 监听下拉框变化事件
            $supplyType.on('change', function() {
                $goodsId.selectPageRefresh();;
                $goodsId.selectPageClear();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
